using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ProjectGreenBox
{
    class WootInfo:StringHelper
    {
        private string m_ItemTitle;
        private string m_ItemImageUrl;
        private string m_ItemPrice;
        private string m_ItemDiscussUrl;
        private string m_ItemPurchaseUrl;
        private string m_ItemCondition;
        private int m_ItemQty;
        private bool   m_ItemSoldOut;
        private bool   m_IsWootOff;

        public WootInfo(){
            WebHelper wh = new WebHelper();
            string strRawText = wh.GetRawData("http://www.woot.com");
            
            ItemTitle = ParseItemTitle(strRawText);
            ItemPrice = ParseItemPrice(strRawText);
            ItemCondition = ParseItemCondition(strRawText);
            ItemDiscussUrl = ParseDicussUrl(strRawText);
            ItemImageUrl = ParseImgUrl(strRawText);
            ItemQty = ParseItemQty(strRawText);
            ItemPurchaseUrl = ParsePurchaseUrl(strRawText);
            ItemSoldOut = ParseItemSoldOut(strRawText);
            IsWootOff = ParseIsWootOff(strRawText);
        }

        public override string ToString(){
            return (ItemTitle + "," + ItemPrice + "," + ItemCondition + "," + ItemDiscussUrl + "," + ItemImageUrl + "," + ItemQty);
        }

        private string ParseItemTitle(string strRawText)
        {
            string strRegEx = "<h3 id=\"TitleHeader\">";
            string strEndRegEx = "</h3>";   
            return InnerText(strRawText, strRegEx, strEndRegEx);
        }

        private string ParseItemPrice(string strRawText)
        {
            string strRegEx = "<span id=\"PriceSpan\">";
            string strEndRegEx = "</span>";  
            return InnerText(strRawText, strRegEx, strEndRegEx);
        }

        private string ParseItemCondition(string strRawText)
        {
            string strRegEx = "<dd id=\"ConditionSpan\">";
            string strEndRegEx = "</dd>";  
            return InnerText(strRawText, strRegEx, strEndRegEx);
        }
        
        private bool ParseItemSoldOut(string strRawText)
        {
            string strRegEx = "ctl00_ContentPlaceHolder_SoldOutPanel";                       
            return (strRawText.Contains(strRegEx));
        }

        private bool ParseIsWootOff(string strRawText)
        {
            string strRegEx = "ctl00_ContentPlaceHolder_WootOffPanel";
            return (strRawText.Contains(strRegEx));
        }

        private int ParseItemQty(string strRawText)
        {
            try
            {
                //<div class="bar" style='width:
                string strRegEx = "<div class=\"bar\" style='width:";
                string strEndRegEx = "'>";
                string IT = InnerText(strRawText, strRegEx, strEndRegEx);
                IT = IT.Replace(" ", "");
                IT = IT.Replace("%", "");
                if (IT == "")
                {
                    IT = "0";
                }
                return Convert.ToInt16(IT);
            }
            catch
            {
                return 0;
            }
        }

        private string ParseImgUrl(string strRawText)
        {
            string strRegEx = "&quot;)\" src=\"";
            string strEndRegEx = "\" alt=";
            return InnerText(strRawText, strRegEx, strEndRegEx);
        }

        private string ParseDicussUrl(string strRawText)
        {
            string strRegEx = "<a id=\"ctl00_ContentPlaceHolder_ForumsLink\" href=\"";
            string strEndRegEx = "\">";
            return InnerText(strRawText, strRegEx, strEndRegEx);
        }

        private string ParsePurchaseUrl(string strRawText)
        {
            string strRegEx = "\"WantOne.aspx?id=";
            string strEndRegEx = "\">I want one!";
            return "http://www.woot.com/WantOne.aspx?id=" + InnerText(strRawText, strRegEx, strEndRegEx);
        }


        public string ItemTitle
        {
            get { return m_ItemTitle; }
            private set { m_ItemTitle = value; }
        }

        public string ItemImageUrl
        {
            get { return m_ItemImageUrl; }
            private set { m_ItemImageUrl = value; }
        }

        public bool ItemSoldOut
        {
            get { return m_ItemSoldOut; }
            private set { m_ItemSoldOut = value; }
        }

        public bool IsWootOff
        {
            get { return m_IsWootOff; }
            private set { m_IsWootOff = value; }
        }


        public string ItemPurchaseUrl
        {
            get { return m_ItemPurchaseUrl; }
            private set { m_ItemPurchaseUrl = value; }
        }

        public string ItemPrice
        {
            get { return m_ItemPrice; }
            private set { m_ItemPrice = value; }
        }

        public string ItemDiscussUrl
        {
            get { return m_ItemDiscussUrl; }
            private set { m_ItemDiscussUrl = value; }
        }

        public string ItemCondition
        {
            get { return m_ItemCondition; }
            private set { m_ItemCondition = value; }
        }

        public int ItemQty
        {
            get { return m_ItemQty; }
            private set { m_ItemQty = value; }
        }        
    }
}
